package com.wust.supermarket.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wust.supermarket.dao.MemberDao;
import com.wust.supermarket.dao.MerchDao;
import com.wust.supermarket.dao.SaleDao;
import com.wust.supermarket.domain.Member;
import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.domain.Sale;
import com.wust.supermarket.service.SaleService;
import com.wust.supermarket.utils.TimeUtil;

@Service
public class SaleServiceImpl implements SaleService {

	@Autowired
	private SaleDao saleDao;
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private MerchDao merchDao;

	@Override
	// 传入的sale的id需要修改为时间加随机值
	public boolean addlaji(Sale sale) {
		// TODO Auto-generated method stub
		// 获取此次交易的saleId
		String idString = TimeUtil.InstockInitialization();
		
			// 获取提交的该条订单的商品实体类
			Merch merch = merchDao.getMerchById(sale.getMerch().getMerchId());
			// 如果没有此商品
			if (merch == null) {
				System.out.println(1);
				return false;
			}
			// 获取商品库存数量
			int merchNumber = merch.getMerchNumber();
			// 如果商品库存数量小于需要得数量
			if (merchNumber < sale.getMerchNumber()) {
				System.out.println(2);
				return false;
			}

			// 获取提交的该条订单的用户id
			int memberId=sale.getMember().getMemberId();
			// 如果不为0  需判断值是否存在（有没有该用户）
			if (memberId != 0) {
				// 如果没有该用户
				if (memberDao.getMemberById(memberId) == null) {
					System.out.println(3);
					return false;
				}
				else {
					// 开始插入
					// 商品表中减去售出数量
					merch.setMerchNumber(merchNumber - sale.getMerchNumber());
					merchDao.updateMerch(merch);
					// 设置saleId
					sale.setSaleId(idString);
					//插入sale
					saleDao.insertSale(sale);
					//添加积分
					//获取member表中对应member数据
					Member member2=memberDao.getMemberById(memberId);
					float oldIntegral=member2.getMemberIntegral();
					float newIntegral=oldIntegral+sale.getSalePrice();
					member2.setMemberIntegral(newIntegral);
					memberDao.updateMember(member2);
				}
				//memberId为0（说明是路人）
			}else {
				// 商品表中减去售出数量
				merch.setMerchNumber(merchNumber - sale.getMerchNumber());
				merchDao.updateMerch(merch);
				// 设置saleId
				sale.setSaleId(idString);
				//插入sale
				saleDao.insertSale(sale);
			}
		
		return true;
	}
	
	
	@Override
	// 传入的sale的id需要修改为时间加随机值
	public boolean completeOneSale(List<Sale> sales) {
		// TODO Auto-generated method stub
		// 获取此次交易的saleId
		String idString = TimeUtil.InstockInitialization();
		for (Sale sale : sales) {
			// 获取提交的该条订单的商品实体类
			Merch merch = merchDao.getMerchById(sale.getMerch().getMerchId());
			// 如果没有此商品
			if (merch == null)
				return false;
			// 获取商品库存数量
			int merchNumber = merch.getMerchNumber();
			// 如果商品库存数量小于需要得数量
			if (merchNumber < sale.getMerchNumber())
				return false;

			// 获取提交的该条订单的用户id
			int memberId=sale.getMember().getMemberId();
			// 如果不为0  需判断值是否存在（有没有该用户）
			if (memberId != 0) {
				// 如果没有该用户
				if (memberDao.getMemberById(memberId) == null)
					return false;
				else {
					// 开始插入
					// 商品表中减去售出数量
					merch.setMerchNumber(merchNumber - sale.getMerchNumber());
					merchDao.updateMerch(merch);
					// 设置saleId
					sale.setSaleId(idString);
					//插入sale
					saleDao.insertSale(sale);
					//添加积分
					//获取member表中对应member数据
					Member member2=memberDao.getMemberById(memberId);
					float oldIntegral=member2.getMemberIntegral();
					float newIntegral=oldIntegral+sale.getSalePrice();
					member2.setMemberIntegral(newIntegral);
					memberDao.updateMember(member2);
				}
				//memberId为0（说明是路人）
			}else {
				// 商品表中减去售出数量
				merch.setMerchNumber(merchNumber - sale.getMerchNumber());
				merchDao.updateMerch(merch);
				// 设置saleId
				sale.setSaleId(idString);
				//插入sale
				saleDao.insertSale(sale);
			}
		}
		return true;
	}

	@Override
	public boolean deleteOneSaleLine(String saleId, int memberId, int merchId) {
		// TODO Auto-generated method stub
		return saleDao.deleteSaleBySaleIdAndMemberIdAndMerchId(saleId, memberId, merchId) > 0;
	}

	@Override
	public boolean deleteSaleBySaleId(String saleId) {
		// TODO Auto-generated method stub
		return saleDao.deleteSaleBySaleId(saleId) > 0;
	}

	@Override
	public boolean deleteSaleBySaleIdAndMemberId(String saleId, int memberId) {
		// TODO Auto-generated method stub
		return saleDao.deleteSaleBySaleIdAndMemberId(saleId, memberId) > 0;
	}

	@Override
	public boolean deleteSaleBySaleIdAndMerchId(String saleId, int merchId) {
		// TODO Auto-generated method stub
		return saleDao.deleteSaleBySaleIdAndMerchId(saleId, merchId) > 0;
	}

	@Override
	public boolean updateSale(Sale sale) {
		// TODO Auto-generated method stub
		return saleDao.updateSale(sale) > 0;
	}

	@Override
	public List<Sale> selectALlSales() {
		// TODO Auto-generated method stub
		List<Sale> sales = saleDao.selectALlSales();
		return sales;
	}

	@Override
	public List<Sale> selectSaleById(String saleId) {
		// TODO Auto-generated method stub
		List<Sale> sales = saleDao.selectSaleById(saleId);
		return sales;
	}

	@Override
	public List<Sale> selectSaleByMemberId(int memberId) {
		// TODO Auto-generated method stub
		List<Sale> sales = saleDao.selectSaleByMemberId(memberId);
		return sales;
	}

	@Override
	public List<Sale> selectSaleByMerchId(int merchId) {
		// TODO Auto-generated method stub
		List<Sale> sales = saleDao.selectSaleByMerchId(merchId);
		return sales;
	}

	@Override
	public Sale selectSaleBySaleIdAndMemberIdAndMerchId(String saleId, int memberId, int merchId) {
		// TODO Auto-generated method stub
		Sale sale = saleDao.selectSaleBySaleIdAndMemberIdAndMerchId(saleId, memberId, merchId);
		return sale;
	}


	@Override
	public float getAllSalePrice() {
		// TODO Auto-generated method stub
		List<Sale> sales= saleDao.selectALlSales();
		float allInteger=0f;
		for (Sale sale:sales) {
			allInteger+=sale.getSalePrice();
		}
		return allInteger;
	}
}
